<!--头部公共部分-->
{include file="common/header" /}
<div style="padding:15px;">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>课程：</label>
                <div class="layui-input-inline">
                        <input type="text" name="coures_name" lay-verify="required" autocomplete="off" readonly
                               class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>班主任：</label>
                <div class="layui-input-inline">
                    <input type="text" lay-verify="required" name="headmaster" style="cursor: pointer" id="banzhuren" autocomplete="off" readonly
                           placeholder="点击选择班主任" class="layui-input banzhuren">
                    <input type="hidden" id="master_id" name="user_master_id">
                </div>
            </div>
            <div class="layui-inline" style="margin-left: 120px;">
                <div class="layui-input-inline">
                    <input type="checkbox"  value= 1  name="holiday_status" title="是否跳过节假日?" lay-skin="primary" >
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>任课老师：</label>
                <div class="layui-input-inline">
                    <input type="text" name="teacher" style="cursor: pointer" id="renkelaoshi" autocomplete="off" readonly
                           placeholder="点击选择老师" class="layui-input renkelaoshi">
                    <input type="hidden" id="teacher_id" name="teacher_id">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">助教：</label>
                <div class="layui-input-inline">
                    <input type="text" name="assistant" style="cursor: pointer" id="zhujiao" autocomplete="off" readonly
                           placeholder="点击选择助教" class="layui-input zhujiao">
                    <input type="hidden" id="assistant_id" name="assistant_id">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>上课日期：</label>
                <div class="layui-input-inline">
                    <input type="text" name="class_start_date" lay-verify="required" id="class_date" autocomplete="off"
                           placeholder="上课日期" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>上课教室：</label>
                <div class="layui-input-inline">
                    <select name="class_room" lay-verify="required" lay-search="">
                        <option value=""></option>
                        {volist name="classroomlist" id="v"}
                        <option value="{$v.id}">{$v['class_name']}</option>
                        {/volist}
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>开始时间：</label>
                <div class="layui-input-inline">
                    <input type="text" name="start_time" lay-verify="required" id="class_time" autocomplete="off"
                           placeholder="开始时间"   class="layui-input">
                </div>
            </div>
            <div class="layui-inline" >
                <label class="layui-form-label"><span class="x-red">*</span>上课时长：</label>
                <div class="layui-input-inline" style="width: 94px">
                    <select name="duration" lay-verify="required" lay-filter="business" >
                        <option value="0">0小时</option>
                        <option value="1">1小时</option>
                        <option value="2">2小时</option>
                        <option value="3">3小时</option>
                        <option value="4">4小时</option>
                        <option value="5">5小时</option>
                        <option value="6">6小时</option>
                    </select>
                </div>
                <div class="layui-input-inline" style="width: 94px;margin-left: -10px;">
                    <select name="minute" lay-verify="required" lay-filter="business" >
                        <option value="0">00分</option>
                        <option value="5">05分</option>
                        <option value="10">10分</option>
                        <option value="15">15分</option>
                        <option value="20">20分</option>
                        <option value="25">25分</option>
                        <option value="30">30分</option>
                        <option value="35">35分</option>
                        <option value="40">40分</option>
                        <option value="45">45分</option>
                        <option value="50">50分</option>
                        <option value="55">55分</option>
                    </select>
                </div>

            </div>

        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>结束时间：</label>
                <div class="layui-input-inline">
                    <input type="text"  style="cursor: pointer" id="end_time" autocomplete="off" readonly
                            class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label"><span class="x-red">*</span>校区名称：</label>
                <div class="layui-input-inline">
                    <select name="campus_id" lay-verify="required">
                        <option value="{$campus_id}">{$campus_name}</option>
                        <!--
                        {foreach name="campuslist" id="vo"}
                        <option value="{$vo.id}">{$vo.campus_name}</option>
                        {/foreach}
                        -->
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item">
            <fieldset class="layui-elem-field " style=" border-color: #00b7ee;border-width: 3px;">
                <legend style="font-size: 18px;color: #00b7ee">学员列表</legend>
                <table class="layui-table" style="width: 795px;margin-left: 5px;" id="showTb">
                    <thead>
                <script>

                    var student_name_arr = eval('{$student_name_arr}');//学生列表
                    /*
                    for(var i = 0; i < student_name_arr.length; i++){
                        document.write('<tr>');
                        document.write('<td ><span style="color: #1E9FFF;float: left">'+student_name_arr[i]+'</span></td>');
                        document.write('</tr>');
                    }
                    */

                    for(var i = 0; i < student_name_arr.length;){
                        document.write('<tr>');
                        for(var j = 0; j  < 4; j ++){
                            if((i+j) < student_name_arr.length){
                                document.write('<td ><span style="color: #1E9FFF;">'+student_name_arr[i+j]+'</span></td>');
                            }else {
                                document.write('<td ><span style="color: #1E9FFF;">---</span></td>');
                            }
                        }
                        document.write('</tr>');
                        i = i+4;
                    }

                </script>
                    </thead>
                </table>
            </fieldset>

        </div>
        <div class="layui-form-item" style="float: right">
            <div class="layui-input-block">
                <input class="layui-btn layui-btn-normal" lay-submit lay-filter="form" value="立即提交" readonly>
                <a class="layui-btn  layui-btn-primary"  id="back">返回</a>
            </div>
        </div>
    </form>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    //填充任课老师
    function renkelaoshi(v) {
        console.log(v)
        console.log(v.field.teacher)
        $(".renkelaoshi").val(v.field.teacher);
        $("#teacher_id").val(v.field.teacher_id);
    }

    //班主任
    function banzhuren(v) {
        console.log(v)
        console.log(v.field.master_name)
        $(".banzhuren").val(v.field.master_name);
        $("#master_id").val(v.field.master_id);
    }


    //填充助教
    function zhujiao(v) {
        console.log(v)
        console.log(v.field.teacher)
        $(".zhujiao").val(v.field.teacher);
        $("#assistant_id").val(v.field.assistant_id);
    }


    layui.use(['table', 'jquery', 'form', 'element','layer', 'laydate'], function () {
        let $ = layui.jquery
            , layer = layui.layer
            , element = layui.element
            , table = layui.table
            , laydate = layui.laydate
            , form = layui.form;


        var campus_id = '{$campus_id}';

        var date=new Date();
        var h=date.getHours(); //获取小时
        var m=date.getMinutes(); //获取分钟
        var s=date.getSeconds(); //获取秒
        if(h < 10){
            h = '0' + h;
        }if(m < 10){
            m = '0' + m;
        }
        if(s < 10){
            s = '0' + s;
        }
        //时间
        laydate.render({
            elem: '#class_time'
            ,value: h+':'+m+':'+s
            ,type: 'time'
            ,trigger: 'click'
            ,change: function(value, date){ //监听日期被切换
                let start_time = value;
                let time_long = (parseInt($('select[name=duration] option:selected').val()))*60 + parseInt($('select[name=minute] option:selected').val());
                let now = new Date();
                let year = now.getFullYear();
                let month = now.getMonth() + 1;
                let date_ = now.getDate();

                if(month < 10){
                    month = "0"+month;
                }
                if(date_ < 10){
                    date_ = "0"+date_;
                }

                let s  = year+"-"+month+"-"+date_+' '+ start_time;
                let mydate= new Date(s);
                mydate.setMinutes(mydate.getMinutes()+ time_long); //当前时间加分钟
                let arr_date = (mydate+"").split(":");
                let str = arr_date[0].substring(arr_date[0].length-2,arr_date[0].length)+ ":" + arr_date[1] + ":" + arr_date[2].substring(0,2);
                $("#end_time").val(str);
            }
        });

        //开班日期
        laydate.render({
            elem: '#class_date',
            type: 'date',
            value:  getNowFormatDate(),
            trigger: 'click'
        });

        //选择老师
        $("#renkelaoshi").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '选择老师',
                area: ['90%', '85%'],
                shadeClose: false,
                content: "{:url('Grades/teacher')}?campus_id="+campus_id,
            });
        })

        //班主任
        $("#banzhuren").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '选择班主任',
                area: ['90%', '85%'],
                shadeClose: false,
                content: "{:url('Grades/headmaster')}?campus_id="+campus_id,
            });
        })

        //选择助教
        $("#zhujiao").click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '选择助教',
                area:  ['90%', '85%'],
                shadeClose: false,
                content: "{:url('Grades/assistant')}?campus_id="+campus_id,
            });
        })

        form.on('select(business)', function(data){
            let start_time = $("#class_time").val();
            let time_long = (parseInt($('select[name=duration] option:selected').val()))*60 + parseInt($('select[name=minute] option:selected').val());
            let now = new Date();
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            let date = now.getDate();

            if(month < 10){
                month = "0"+month;
            }
            if(date < 10){
                date = "0"+date;
            }

            let s  = year+"-"+month+"-"+date+' '+ start_time;
            let mydate= new Date(s);
            mydate.setMinutes(mydate.getMinutes()+ time_long); //当前时间加分钟
            let arr_date = (mydate+"").split(":");
            let str = arr_date[0].substring(arr_date[0].length-2,arr_date[0].length)+ ":" + arr_date[1] + ":" + arr_date[2].substring(0,2);
            $("#end_time").val(str);
        })


        //返回
        $('#back').click(function () {

            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        });
        //监听提交
        form.on('submit(form)', function (data) {

            var ids = '{$ids}'; // infostudents表的自增ID
            var count = parseInt('{$count}')+1; //班级名字条数
            var field = data.field;
            if(field.holiday_status != 1){
                field.holiday_status = 0;
            }
            var course_id = '{$course_id}'; // 课程ID
            var class_end_date = '';//结束日期
            var timestamp = (new Date()).getTime(); //获取当前时间戳避免班级名字重复
            var grades_name = field.coures_name  + '_补课班级_'+count + '_班';
            var interval_value = "0"; //间隔
            var pos = "1";  //下标
            var preoccupation = ""; //欲招人数
            var project_num = "1";
            var remarks = ""; //备注
            var repeat_status = "2";//分班类型


            var classdata = [];
            let start_time = field.start_time;
            let time_long = (parseInt($('select[name=duration] option:selected').val()))*60 + parseInt($('select[name=minute] option:selected').val());
            let now = new Date();
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            let date = now.getDate();

            if(month < 10){
                month = "0"+month;
            }
            if(date < 10){
                date = "0"+date;
            }

            let s  = year+"-"+month+"-"+date+' '+ start_time;
            let mydate= new Date(s);
            mydate.setMinutes(mydate.getMinutes()+ time_long); //当前时间加分钟
            let arr_date = (mydate+"").split(":");
            let str = arr_date[0].substring(arr_date[0].length-2,arr_date[0].length)+ ":" + arr_date[1] + ":" + arr_date[2].substring(0,2);

            console.log(str)
            classdata = eval(classdata);
            var row = {};
            row.weekday = field.class_start_date;
            row.start_time = field.start_time;
            row.end_time = str;
            row.room_text = $('select[name=class_room] option:selected').text();
            row.room_id = $('select[name=class_room] option:selected').val();
            row.pos = 1;
            classdata.push(row);
            var classdata_ = JSON.stringify(classdata);

            /****提交数据**/
            field.classdata = classdata_;
            field.course_id = course_id;
            field.class_end_date = class_end_date;
            field.grades_name = grades_name;
            field.interval_value = interval_value;
            field.pos = pos;
            field.preoccupation = preoccupation;
            field.project_num = project_num;
            field.remarks = remarks;
            field.repeat_status = repeat_status;


            var isLoad;
            $.ajax({
                url: '{:url("Remedial/addclassdata")}?status='+2 + "&conflict=" + 2,
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    console.log("data:"+result.data);
                    if(result.data >= 0){
                        //  layer.msg(result.msg);
                        var title = result.msg;
                        console.log(title)
                        var url;
                        if(result.data === 10001){
                            url = '{:url("Remedial/addclassdata")}?status='+ 2 +  "&conflict=" + 3;
                        }else{
                            url = '{:url("Remedial/addclassdata")}?status='+ 1  + "&conflict=" + 2;
                        }
                        layer.confirm(title, function (index) {
                            $.ajax({
                                url: url,
                                method: "post",
                                data: field,
                                beforeSend: function () {
                                    isLoad = layer.load(0, {
                                        shade: false
                                    }); //0代表加载的风格，支持0-2
                                },
                                success: function (result) {
                                    if(result.data >= 0){
                                        layer.confirm(result.msg, function (index) {
                                            if(result.data != 10001){
                                                //   location.href = result.url;
                                                url = '{:url("Remedial/addclassdata")}?status='+ 1  + "&conflict=" + 3;
                                                $.ajax({
                                                    url: url,
                                                    method: "post",
                                                    data: field,
                                                    beforeSend: function () {
                                                        isLoad = layer.load(0, {
                                                            shade: false
                                                        }); //0代表加载的风格，支持0-2
                                                    },
                                                    success: function (result) {
                                                //        layer.msg(result.msg);
                                                            if (result.code === 1) {
                                                                //创建成功插入表
                                                                var field_ = {};
                                                                field_.ids = ids;
                                                                field_.campus_id = campus_id;
                                                                field_.grades_name = grades_name; //班级名称
                                                                $.ajax({
                                                                    url: '/admin/remedial/insert_student_infos',
                                                                    method: "post",
                                                                    data: field_,
                                                                    beforeSend: function () {
                                                                        isLoad = layer.load(0, {
                                                                            shade: false
                                                                        }); //0代表加载的风格，支持0-2
                                                                    },
                                                                    success: function (result) {
                                                                        layer.msg(result.msg);
                                                                        if (result.code === 1) {
                                                                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                                            parent.layer.close(index); //再执行关闭
                                                                        }

                                                                    },
                                                                    complete: function () {
                                                                        layer.close(isLoad)
                                                                    },
                                                                    error: function (error) {
                                                                        console.log(error)
                                                                    }
                                                                });
                                                            }


                                                    },
                                                    complete: function () {
                                                        layer.close(isLoad)
                                                    },
                                                    error: function (error) {
                                                        console.log(error)
                                                    }
                                                });


                                            }

                                        });
                                        return;
                                    }

                             //       layer.msg(result.msg);
                                        if (result.code === 1) {
                                            //创建成功插入表
                                            var field_ = {};
                                            field_.ids = ids;
                                            field_.campus_id = campus_id;
                                            field_.grades_name = grades_name; //班级名称
                                            $.ajax({
                                                url: '/admin/remedial/insert_student_infos',
                                                method: "post",
                                                data: field_,
                                                beforeSend: function () {
                                                    isLoad = layer.load(0, {
                                                        shade: false
                                                    }); //0代表加载的风格，支持0-2
                                                },
                                                success: function (result) {
                                                    layer.msg(result.msg);
                                                    if (result.code === 1) {
                                                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                        parent.layer.close(index); //再执行关闭
                                                    }

                                                },
                                                complete: function () {
                                                    layer.close(isLoad)
                                                },
                                                error: function (error) {
                                                    console.log(error)
                                                }
                                            });
                                        }


                                },
                                complete: function () {
                                    layer.close(isLoad)
                                },
                                error: function (error) {
                                    console.log(error)
                                }
                            });
                        });
                        return;

                    }
             //       layer.msg(result.msg);
                    if (result.code === 1) {
                        //创建成功插入表
                        var field_ = {};
                        field_.ids = ids;
                        field_.campus_id = campus_id;
                        field_.grades_name = grades_name; //班级名称
                        $.ajax({
                            url: '/admin/remedial/insert_student_infos',
                            method: "post",
                            data: field_,
                            beforeSend: function () {
                                isLoad = layer.load(0, {
                                    shade: false
                                }); //0代表加载的风格，支持0-2
                            },
                            success: function (result) {
                                layer.msg(result.msg);
                                if (result.code === 1) {
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                }

                            },
                            complete: function () {
                                layer.close(isLoad)
                            },
                            error: function (error) {
                                console.log(error)
                            }
                        });
                    }

                    if(result.code === 0){
                        layer.msg(result.msg);
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;
        });


        let start_time = $("#class_time").val();
        let time_long = (parseInt($('select[name=duration] option:selected').val()))*60 + parseInt($('select[name=minute] option:selected').val());
        let now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth() + 1;
        let date__ = now.getDate();

        if(month < 10){
            month = "0"+month;
        }
        if(date__ < 10){
            date__ = "0"+date__;
        }

        let s_  = year+"-"+month+"-"+date__+' '+ start_time;
        let mydate_= new Date(s_);
        mydate_.setMinutes(mydate_.getMinutes()+ time_long); //当前时间加分钟
        let arr_date = (mydate_+"").split(":");
        let str = arr_date[0].substring(arr_date[0].length-2,arr_date[0].length)+ ":" + arr_date[1] + ":" + arr_date[2].substring(0,2);
        $("#end_time").val(str);

    });



    //获取当前时间，格式YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
</script>